package base.view

import android.os.Bundle
import androidx.annotation.LayoutRes
import base.contract.DialogConvertListener
import base.view.ui.BaseDialog
import base.utils.DialogViewHolder

/**
 * @author njmsir
 * Created by njmsir on 2017/9/18.
 */
class CommonDialog : BaseDialog() {

    companion object {
        fun init(): CommonDialog {
            return CommonDialog()
        }
    }

    private var convertListener: DialogConvertListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (savedInstanceState != null) {
            convertListener = savedInstanceState.getParcelable(LISTENER)
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putParcelable(LISTENER, convertListener)
    }

    override fun convertView(holder: DialogViewHolder, dialog: BaseDialog) {
        convertListener?.convertView(holder, dialog)
    }

    override fun dialogLayoutId(): Int {
        return layoutId
    }

    fun setLayoutId(@LayoutRes layoutId: Int): CommonDialog {
        this.layoutId = layoutId
        return this
    }

    fun setConvertListener(convertListener: DialogConvertListener): CommonDialog {
        this.convertListener = convertListener
        return this
    }

    override fun onDestroy() {
        super.onDestroy()
        convertListener = null
    }
}